package logic

import (
	"context"
	"fmt"
	"zc2/model/mysql"
	"zc2/model/redis"

	"zc2/userrpc/internal/svc"
	"zc2/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeGoodsLogic {
	return &LikeGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LikeGoodsLogic) LikeGoods(in *userrpc.LikeGoodsRequest) (*userrpc.LikeGoodsResponse, error) {
	// todo: add your logic here and delete this line
	var goods mysql.Product
	mysql.DB.Where("id=?", in.Id).Find(&goods)
	if goods.ID == 0 {
		return nil, fmt.Errorf("没有该商品，请重新输入")
	}
	if goods.UserId != int(in.Userid) {
		return nil, fmt.Errorf("该用户没有该商品，请重新输入")
	}
	redis.Client.ZIncrBy("likegoodslist", 1, goods.Title)
	return &userrpc.LikeGoodsResponse{}, nil
}
